package com.oocl.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.entity.Clients;
import com.oocl.entity.DeliverInfo;
import com.oocl.entity.Foods;
import com.oocl.entity.Merchants;
import com.oocl.entity.OrderComment;
import com.oocl.entity.Orders;
import com.oocl.entity.SessionInfo;
import com.oocl.service.ClientsService;
import com.oocl.service.DeliverInfoService;
import com.oocl.service.FirstPageService;
import com.oocl.service.FoodsService;
import com.oocl.service.MerchantsService;
import com.oocl.service.OrderCommentService;
import com.oocl.service.OrdersService;

@Controller

public class PayOrderController {

	@Resource(name="ordersService")
	private OrdersService ordersService;
	@Resource(name="clientsService")
	private ClientsService clientsService;
	@Resource(name="deliverInfoService")
	private DeliverInfoService deliverInfoService;
	@Resource(name="merchantsService")
	private MerchantsService merchantsService;
	@Resource(name="foodsService")
	private FoodsService foodsService;
	@Resource(name="orderCommentService")
	private OrderCommentService orderCommentService;
	@Resource(name="firstPageService")
	private FirstPageService firstPageService;
	/*
	 * @Method:saveAddr
	 * @fuction:insert addr info as deliver info 
	 * @params:DeliverInfo d,Model model,HttpSession session
	 * @author:wendy
	 * @time:2017-08-13
	 */
	
	@ResponseBody
	@RequestMapping(value="newaddr",method=RequestMethod.POST)
	public List<DeliverInfo> saveAddr(@RequestBody DeliverInfo d,Model model,HttpSession session){
		//从session中获取用户id
		System.out.println(d.getAddr()+"..."+d.getContact()+"..."+d.getTel());
		String cid=(String)session.getAttribute("cid");
		Clients client=new Clients();
		client=clientsService.findClientsByCid(cid);
		DeliverInfo dinfo=new DeliverInfo();
		dinfo.setAddr(d.getAddr());
		dinfo.setContact(d.getContact());
		dinfo.setTel(d.getTel());
		dinfo.setClient(client);
		deliverInfoService.addDeliverInfo(dinfo, cid);
		List<DeliverInfo> dinfolist=new ArrayList<DeliverInfo>();
		dinfolist=deliverInfoService.loadAllDeliverInfoByCid(cid);
		return dinfolist;
	}
	
	/*
	 * @Method:addOrders
	 * @fuction:insert cart food list as orders 
	 * @params:DeliverInfo deliverInfo, Model model,HttpSession session
	 * @author:wendy
	 * @time:2017-08-13
	 */
//	@ResponseBody
	@RequestMapping(value="addorders",method=RequestMethod.GET)
	public String addOrders(String did, Model model,HttpSession session){
		
		System.out.println("add orders in");
		
		//从session中获取商家名及购物车内容，用于生成订单
		String icnum=(String)session.getAttribute("icnum");
		System.out.println("商家"+icnum);
		
		//获取购物车菜品，用于生成订单及页面显示
		List<SessionInfo> flistFromSession=(List<SessionInfo>)session.getAttribute("cart");
		//System.out.println("菜品列表"+flistFromSession.size());
		
		//从Session中获取用户id
		String cid=(String)session.getAttribute("cid");
		//cid转client，用于页面显示
		Clients client=new Clients();
		client=clientsService.findClientsByCid(cid);
		System.out.println("用户"+cid);
		
		//获取订单的配送地址信息，用于生成订单
		DeliverInfo deliverInfo=new DeliverInfo();
		deliverInfo=deliverInfoService.loadDeliverInfoById(did);
		//deliverInfo=deliverInfoService.loadDeliverInfoById(d.getDid());
		
		//用户订单入库并获取用户订单
		List<Orders> orderslist=new ArrayList<Orders>();
		String oid=ordersService.addOrderList(deliverInfo,icnum,cid,flistFromSession);
		orderslist=ordersService.loadOrdersByOid(oid);
		System.out.println(orderslist.size());
		
		//获取订单评论
		OrderComment orderComment=new OrderComment();
		orderComment=orderCommentService.loadOrderCommentByOid(oid);
		String comment=null;
		if(orderComment!=null){
			comment=orderComment.getComments();			
		}
		
		
		String orderstate=orderslist.get(0).getOstate();//orderslist.get(0).getOstate()
		
		String info="";
		String moreinfo="";
		
		switch(orderstate){
		case "0":
			info="商家未接单";
			moreinfo="请耐心等待商家接单";
			break;
		case "1":
			info="商家已接单";
			moreinfo="订单即将配送";
			break;
		case "2":
			info="商家拒接";
			moreinfo="很遗憾，因为一些原因，商家不能接您的订单";
			break;
		case "3":
			info="订单正在配送中";
			moreinfo="您的外卖已经在路上了";
			break;
		case "4":
			info="订单已完成";
			moreinfo="很高兴能为您提供本次服务";
			break;
		}
		
		//根据投诉状态判断投诉按钮是否禁用，测试时改为0
		model.addAttribute("compstate", orderslist.get(0).getCompstate());
		
		
		model.addAttribute("client", client);
		//根据订单状态决定评论区域的显示
		model.addAttribute("orderstate", orderstate);
		
		model.addAttribute("sum", orderslist.get(0).getOsum());
		model.addAttribute("merchant", orderslist.get(0).getMerchant());
		model.addAttribute("orderid", oid);  //订单号
		
		model.addAttribute("info", info); //订单状态
		model.addAttribute("moreinfo", moreinfo); //订单状态更多信息
		model.addAttribute("infolist", orderslist);  //订单列表
		//根据不同订单状态显示不同订单信息
		model.addAttribute("dinfo", orderslist.get(0).getDeliverInfo()); //订单配送地址
		//根据该订单是否有评论决定评论区显示的内容，测试时改为null，否则comment
		model.addAttribute("cmt", comment); //订单评论
		
		
		//用户下订单以后，清空购物车
		session.setAttribute("cart", null);
		
		return "orderinfo";
		
	}
	
	
	
	/*
	 * @Method:toLastPage
	 * @fuction:redirect to cart page
	 * @params:Model model,HttpSession session
	 * @author:wendy
	 * @time:2017-08-14
	 */
	@RequestMapping(value="tocart")
	public String toCart(HttpSession ses,Model model){
		String cid=(String)ses.getAttribute("cid");
		String icnum=(String) ses.getAttribute("icnum");
		Merchants m=new Merchants();
		m=merchantsService.loadMerchantsByIcnum(icnum);
		List<Foods> flist=new ArrayList<Foods>();
		flist=foodsService.loadFoodsByIcnum(icnum);
		String levelString = firstPageService.computeRatingsLevelWithIcnum(icnum);
		Merchants merchant = firstPageService.findmerchantsByIcnum(icnum);
		model.addAttribute("levelString",levelString);
		model.addAttribute("foods", flist);
		model.addAttribute("merchant",merchant);
		return "cart";
	}
	
	
	@RequestMapping(value="detail",method=RequestMethod.GET)
	public String orderDetail(String oid,Model model,HttpSession session){
		System.out.println("oid"+oid);
		//从Session中获取用户id
		String cid=(String)session.getAttribute("cid");
		//cid转client，用于页面显示
		Clients client=new Clients();
		client=clientsService.findClientsByCid(cid);
		System.out.println("用户"+cid);

		//获取商家名
		String icnum=(String)session.getAttribute("icnum");
		System.out.println("商家"+icnum);
		
		//获取订单列表
		List<Orders> orderslist=new ArrayList<Orders>();
		orderslist=ordersService.loadOrdersByOid(oid);
		
		//获取订单评论
		OrderComment orderComment=new OrderComment();
		orderComment=orderCommentService.loadOrderCommentByOid(oid);
		String comment=null;
		if(orderComment!=null){
			comment=orderComment.getComments();			
		}
		System.out.println("comment....."+comment);
		
		String info="";
		String moreinfo="";
		
		String ostate=orderslist.get(0).getOstate();
		
		switch(ostate){
		case "0":
			info="商家未接单";
			moreinfo="请耐心等待商家接单";
			break;
		case "1":
			info="商家已接单";
			moreinfo="订单即将配送";
			break;
		case "2":
			info="商家拒接";
			moreinfo="很遗憾，因为一些原因，商家不能接您的订单";
			break;
		case "3":
			info="订单正在配送中";
			moreinfo="您的外卖已经在路上了";
			break;
		case "4":
			info="订单已完成";
			moreinfo="很高兴能为您提供本次服务";
			break;
		}
		
		String compstate=orderslist.get(0).getCompstate();
		System.out.println(compstate);
		
		//根据投诉状态判断投诉按钮是否禁用，测试时改为0
		model.addAttribute("compstate", compstate);
		
		
		model.addAttribute("client", client);
		//根据订单状态决定评论区域的显示
		model.addAttribute("orderstate", ostate);
		
		model.addAttribute("sum", orderslist.get(0).getOsum());
		model.addAttribute("merchant", orderslist.get(0).getMerchant());
		model.addAttribute("orderid", oid);  //订单号
		
		model.addAttribute("info", info); //订单状态
		model.addAttribute("moreinfo", moreinfo); //订单状态更多信息
		model.addAttribute("infolist", orderslist);  //订单列表
		//根据不同订单状态显示不同订单信息
		model.addAttribute("dinfo", orderslist.get(0).getDeliverInfo()); //订单配送地址
		//根据该订单是否有评论决定评论区显示的内容，测试时改为null，否则comment
		model.addAttribute("cmt", comment); //订单评论
		model.addAttribute("ordercmt", orderComment);
		
		
		return "orderinfo";
		
	}
}
